<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use User;

class UserController extends Controller
{
    //
    public function __construct()
    {
        $this->middleware('auth');
    }
    //添加用户
    public function addUser(Request $request){
        $data=$request->all();
        $user=new User();
        $user->name=$data['name'];
        $user->phone=$data['phone'];
        $user->idcard=$data['idcard'];
        $user->userid=$data['userid'];
        $user->username=$data['username'];
        if(empty(User::where('idcard',$user->idcard)->first())){
            $user->save(); 
            return redirect('/userlist');
        }else
        {
            print('客户已经添加,如有疑问联系管理员');
        }
    }
    
    //用户列表
    public function userList(Request $request){
        $data=$request->all();
        $user=$request->user();
        if($user->role=='admin'){
            $users=DB::table('users')->simplePaginate(6);
        }
        return view('userlist')->with('users',$users);
    }
}
